package cn.chengpengper.shortlink.controller;

import cn.chengpengper.shortlink.controller.request.AccountLoginRequest;
import cn.chengpengper.shortlink.controller.request.AccountRegisterRequest;
import cn.chengpengper.shortlink.enums.BizCodeEnum;
import cn.chengpengper.shortlink.service.AccountService;
import cn.chengpengper.shortlink.service.FileService;
import cn.chengpengper.shortlink.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huchengpeng
 * @since 2023-08-20
 */
@RestController
@RequestMapping("/api/account/")
public class AccountController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    @PostMapping("v1/uploadUserAvatar")
    public JsonData uploadUserAvatar(@RequestParam("file") MultipartFile file) {
        String url = fileService.uploadUserAvatar(file);
        if (StringUtils.isBlank(url)) {
            return JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
        }
        return JsonData.buildSuccess(url);
    }

    @PostMapping("v1/register")
    public JsonData register(@RequestBody AccountRegisterRequest registerRequest) {
        return accountService.register(registerRequest);
    }

    @PostMapping("v1/login")
    public JsonData login(@RequestBody AccountLoginRequest loginRequest) {
        return accountService.login(loginRequest);
    }

}

